package test
import grails.converters.JSON

class ApplicationController {

	def getOrganization(params){
		//find organization
		//Organization inorg = Organization.get(params.get('orgid'))
        //println ${params}
		Organization inorg = Organization.getAll()[0]
		return inorg
	}
	
	def getDepartmentsByOrg(params){
		Organization inorg = Organization.get(params.get('orgid'))
		return Department.findAllByOrganization(inorg)
	}
	
	def getProjectByDep(params){
		Department dep = Department.get(params.get('depid'))
		return Project.findAllByDepartment(dep)
	}
	
	def getDevelopersbyPrj(params){
		Project prj = Project.get(params.get('prjid'))
		return Developer.findAllByProject(prj)
	}
	
	def getDepartmentTreeByOrg(){
		Organization org = Organization.list()[0]
		return Department.findAllByOrganization(org).collect{[depid:it.id.toString(),
			text:it.name,
			//leaf: "true",
			children: getProjectTreeByDep(it.id)
		   ]}
	}
	
	def getProjectTreeByDep(def depid){
		Department dep = Department.get(depid)
		return Project.findAllByDepartment(dep).collect{[prjid:it.id.toString(),
									 text:it.name,
                //text:it.surname,
									 //leaf: "false",
									 children: getDeveloperTreeByPrj(it.id)]}
	}
	
	def getDeveloperTreeByPrj(def prjid){
		Project prj = Project.get(prjid)
		return Developer.findAllByProject(prj).collect{[devid:it.id.toString(),
									 text:it.name,

									 leaf: "true"]}
	}


	
	def getTree = {

		def res = getDepartmentTreeByOrg()
		render res as JSON
	}
	
	def getGrid = {	
		def orgid = params.getAt('orgid')
		def depid = params.getAt('depid')
		def prjid = params.getAt('prjid')
		def devid = params.getAt('devid')
		if (orgid)
			render Organization.get(orgid).collect{[id:it.id, name:it.name]} as JSON;
		else if(depid)
			render Department.get(depid).collect{[id:it.id, name:it.name]} as JSON;
		else if(prjid)
			render Project.get(prjid).collect{[id:it.id, name:it.name]} as JSON;
		else if(devid)
			render Developer.get(devid).collect{[id:it.id, name:it.name, surname:it.surname]} as JSON;//surname in grid 102
	}

	def create = {
		def name = params.getAt('name')
		def orgid = params.getAt('orgid')
		def depid = params.getAt('depid')
		def prjid = params.getAt('prjid')
		def devid = params.getAt('devid')


		//def action =params.getAt('operation')
		if (name){
			//create dep
			if (orgid && !Department.findWhere(name: name)){ //proverka
				Organization org = Organization.get(orgid)
				Department newdep = new Department()
				newdep.name = name
				newdep.organization = org
				newdep.save()
				def res = ['success':true];
				render res as JSON
			}
			else if(orgid && Department.findWhere(name: name)){
				def res = ['success':false, 'failureType':'SERVER_INVALID','errormsg':'Department already exists'];
				render res as JSON
			}

			//create project
			else if (depid && !Project.findWhere(name: name)){
				Department dep = Department.get(depid)
				Project newprj = new Project()
				newprj.name = name
				newprj.department = dep
				newprj.save()
				def res = ['success':true];
				render res as JSON
			}
			else if(depid && Project.findWhere(name: name)){
				def res = ['success':false, 'failureType':'SERVER_INVALID','errormsg':'Project already exists'];
				render res as JSON
			}

			//create developer
			else if (prjid && !Developer.findWhere(name: name)){
				Project prj = Project.get(prjid)
				Developer newdev = new Developer()
				newdev.name = name
				newdev.project = prj
				newdev.save()
				def res = ['success':true];
				render res as JSON
			}
			else if(prjid && Developer.findWhere(name: name)){
				def res = ['success':false, 'failureType':'SERVER_INVALID','errormsg':'Project already exists'];
				render res as JSON
			}
		}
       //add return
	}
	
	def update = {
		def name = params.getAt('name')
		def orgid = params.getAt('orgid')
		def depid = params.getAt('depid')
		def prjid = params.getAt('prjid')
		def devid = params.getAt('devid')
		//ORGANIZATION
		if (name && orgid){
			if (Organization.exists(orgid)){
				Organization item = Organization.get(orgid)
				Organization tmp = Organization.findWhere(name: name)
				if (item && tmp && item != tmp){
					def res = ['success':false, 'failureType':'SERVER_INVALID','errormsg':'Organization with the same name'];
					render res as JSON
				}
				else{
					item.name = name
					item.save()
					def res = ['success':true];
					render res as JSON
				}
			}
			else{
				def res = ['success':false, 'failureType':'SERVER_INVALID','errormsg':'Organization doesnt exists'];
				render res as JSON
			}
		}
		
		//DEPARTMENT
		else if (name && depid){
			if (Department.exists(depid)){
				Department item = Department.get(depid)
				Department tmp = Department.findWhere(name: name)
				if (item && tmp && item != tmp){
					def res = ['success':false, 'failureType':'SERVER_INVALID','errormsg':'Department with the same name'];
					render res as JSON
				}
				else { 
					item.name = name
					item.save()
					def res = ['success':true];
					render res as JSON
				}
			}
			else{
				def res = ['success':false, 'failureType':'SERVER_INVALID','errormsg':'Department doesnt exists'];
				render res as JSON
			}
		}
		
		//PROJECT
		else if (name && prjid){
			if (Project.exists(prjid)){
				Project item = Project.get(prjid)
				Project tmp = Project.findWhere(name: name)
				if (item && tmp && item != tmp){
					def res = ['success':false, 'failureType':'SERVER_INVALID','errormsg':'Project with the same name'];
					render res as JSON
				}
				else {
					item.name = name
					item.save()
					def res = ['success':true];
					render res as JSON
				}
			}
			else{
				def res = ['success':false, 'failureType':'SERVER_INVALID','errormsg':'Project doesnt exists'];
				render res as JSON
			}
		}

		//DEVELOPER
		else if (name && devid){
			if (Developer.exists(devid)){
				Developer item = Developer.get(devid)
				Developer tmp = Developer.findWhere(name: name)
				if (item && tmp && item != tmp){
					def res = ['success':false, 'failureType':'SERVER_INVALID','errormsg':'Developer with the same name'];
					render res as JSON
				}
				else {
					item.name = name
					item.save()
					def res = ['success':true];
					render res as JSON
				}
			}
			else{
				def res = ['success':false, 'failureType':'SERVER_INVALID','errormsg':'Developer doesnt exists'];
				render res as JSON
			}
		}
		//def res = ['success':false]
		//render res as JSON
	}
	
	def delete = {
		def name = params.getAt('name')
		def orgid = params.getAt('orgid')
		def depid = params.getAt('depid')
		def prjid = params.getAt('prjid')
		def devid = params.getAt('devid')

		//ORGANIZATION
		if (orgid){
			if (Organization.exists(orgid)){
				Organization item = Organization.get(orgid)
				item.delete()
				def res = ['success':true];
				render res as JSON
			}
			else{
				def res = ['success':false, 'failureType':'SERVER_INVALID','errormsg':'Organization doesnt exists'];
				render res as JSON
			}
		}

		//DEPARTMENT
		else if (depid){
			if (Department.exists(depid)){
				Department item = Department.get(depid)
				item.delete()
				def res = ['success':true];
				render res as JSON
			}
			else{
				def res = ['success':false, 'failureType':'SERVER_INVALID','errormsg':'Department doesnt exists'];
				render res as JSON
			}
		}

		//PROJECT
		else if (prjid){
			if (Project.exists(prjid)){
				Project item = Project.get(prjid)
				item.delete()
				def res = ['success':true];
				render res as JSON
			}
			else{
				def res = ['success':false, 'failureType':'SERVER_INVALID','errormsg':'Project doesnt exists'];
				render res as JSON
			}
		}

		//DEVELOPER
		else if (devid){
			if (Developer.exists(devid)){
				Developer item = Developer.get(devid)
				item.delete()
				def res = ['success':true];
				render res as JSON
			}
			else{
				def res = ['success':false, 'failureType':'SERVER_INVALID','errormsg':'Developer doesnt exists'];
				render res as JSON
			}
		}
	}
}